/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				UnitTypeExistsCondition
 
Function						: 				Provides a class for holding a 
												condition that specifies whether 
												a unit exists at a particular location
****************************************************************************/

package planninglanguage.conditions;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;
import base.WargusPlayer;
import base.WargusStateImporter;
import base.WargusUnit;


public class UnitTypeExistsCondition extends Condition {

	int m_playerID;
	String m_unitType;
	
	public UnitTypeExistsCondition(String a_unitType, int a_playerID)
	{
		m_playerID = a_playerID;
		m_unitType = a_unitType;
	}
	public String toString(int tabs) {
		String n = super.toString(tabs);
		n = n+ "Player Id is:" + m_playerID;
		n = n + "Unit Type is" + m_unitType;
		
		return n;
	}	

	public boolean compare(UnitTypeExistsCondition c)
	{
		if(m_playerID==c.m_playerID && m_unitType==c.m_unitType)
			return true;
		else
			return false;
	}
	public String getUnitType()
	{
		return m_unitType;
	}
	public boolean test(WargusGameState a_gameState)
	{
		for(WargusUnit u:a_gameState.getPlayer().getUnits()) {
			if (WargusStateImporter.unitTypeToString(u.getType()).equals(m_unitType)) return true;	
		}
		
		for(WargusPlayer p:a_gameState.getOpponent()) {
			for(WargusUnit u:p.getUnits()) {
				if (WargusStateImporter.unitTypeToString(u.getType()).equals(m_unitType)) return true;	
			}
		}
		return false;
	}
	
	public void writeXML(XMLWriter w) {
		w.tag("condition type=\""+this.getClass().getSimpleName()+"\"");
		{
			w.tag("unit-type", m_unitType);
			w.tag("player-id", m_playerID);
		}
		w.tag("/condition");
	}
	
	protected static Condition loadConditionFromXML(Element e) {
		return new UnitTypeExistsCondition(e.getChildText("unit-type"),
				Integer.parseInt(e.getChildText("player-id")));
	}
}
